import Game from "./Game";
import PlayerData from "./data/PlayerData";



export default class GameStorage extends Laya.Script {

    private static STORAGE_PLAYER: string = 'STORAGE_PLAYER'; //保存本地

    public static loadStorage(callback: any): void {
        let storage = window.localStorage;

        let dataJson = storage.getItem(GameStorage.STORAGE_PLAYER);
 
        if (dataJson) {
            let jsonObj = JSON.parse(dataJson);
            if (jsonObj) {
                // console.log(jsonObj)
                Game.hero.playerData.gold = jsonObj["gold"] ? jsonObj["gold"] : PlayerData.gold;
                Game.hero.playerData.attackSpeedLv = jsonObj["attackSpeedLv"] ? jsonObj["attackSpeedLv"] : PlayerData.attackSpeedLv;
                Game.hero.playerData.diamond = jsonObj["diamond"] ? jsonObj["diamond"] : PlayerData.diamond;
                Game.hero.playerData.mapLevel = jsonObj["mapLevel"] ? jsonObj["mapLevel"] : PlayerData.mapLevel;
                Game.hero.playerData.mapBgId = jsonObj["mapBgId"] ? jsonObj["mapBgId"] : PlayerData.mapBgId;
                Game.hero.playerData.heroId = jsonObj["heroId"] ? jsonObj["heroId"] : PlayerData.heroId;
                Game.hero.playerData.attackLv = jsonObj["attackLv"] ? jsonObj["attackLv"] : PlayerData.attackLv;
                Game.hero.playerData.hpLv = jsonObj["hpLv"] ? jsonObj["hpLv"] : PlayerData.hpLv;
                Game.hero.playerData.heros = jsonObj["heros"] ? jsonObj["heros"] : PlayerData.heros;
            }
            callback && callback(true);
            return;
        }
        callback && callback(false);

    }

    public static saveStorage() {
        let localData: any = {};
        localData["gold"] = Game.hero.playerData.gold;
        localData["attackSpeedLv"] = Game.hero.playerData.attackSpeedLv;
        localData["diamond"] = Game.hero.playerData.diamond;
        localData["mapLevel"] = Game.hero.playerData.mapLevel;
        localData["mapBgId"] = Game.hero.playerData.mapBgId;
        localData["heroId"] = Game.hero.playerData.heroId;
        localData["attackLv"] = Game.hero.playerData.attackLv;
        localData["hpLv"] = Game.hero.playerData.hpLv;
        localData["heros"] = Game.hero.playerData.heros;
        let dataJson = JSON.stringify(localData);
        if (dataJson) {
            let storage = window.localStorage;
            storage.setItem(GameStorage.STORAGE_PLAYER, dataJson);
        }

    }

}